<?php
/**
 * Created by PhpStorm.
 * User: hisheng
 * Date: 2017/6/6
 * Time: 18:02
 */
namespace App\Http\Controllers\Api;

use App\Dao\AlimnsDao;
use App\Dao\UserDao;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    public $user ;
    public $userDao;
    public function __construct(User $user ,UserDao $userDao)
    {
        $this->user = $user;
        $this->userDao = $userDao;
    }
    
    public function code(){
        $mobile = Input::get('mobile');
        if($mobile){
            $code = rand(1000,9999);
            Session::put('code',$code);
            Session::put('mobile',$mobile);
            $alimns = new AlimnsDao();
            $result  = $alimns->run($mobile,$code);
            if($result){
                return $this->json([
                    'code'=>$code
                ]);
            }
        }
    }
    
    public function postAdd($edit = ''){
        $this->insertDefault('mobile');
        if($this->user->mobile){
            $code = Input::get('code');
            if(empty($edit)){
                if(empty($code) || ($code != Session::get('code'))){
                    throw new \Exception('缺少验证码');
                }
            }
            $user = $this->userDao->findByMobile($this->user->mobile);
            if($user){
                $this->user = $user;
            }
            $this->insertDefault('name');
            $this->insertDefault('job');
            $this->insertDefault('city');
            $password = Input::get('password');
            if($password){
                $this->user->password = md5($password);
            }
            $this->user->save();
            $u = $this->user->find($this->user->id);
            if(empty($edit)){
                Session::put('user_id',$this->user->id);
            }
            return $this->json($u->toArray());
            
        }
        return $this->error('缺少参数');
    }
    
    public function edit($id){
        if($id){
            return $this->postAdd(1);
        }
        return $this->error('缺少参数');
    }
    
    
    
    public function login(){
        $mobile = Input::get('mobile');
        $password = Input::get('password');
        $user = $this->userDao->login($mobile,$password);
        if($user){
            Session::put('user_id',$user->id);
            return $this->json($user->toArray());
        }
        $mobileUser = $this->userDao->findByMobile($mobile);
        if($mobileUser){
            return $this->error('密码错误');
        }
        return $this->error('没有此用户');
    }
    
    public function codeLogin(){
        return $this->postAdd();
    }
    
    public function insertDefault($name){
        $v = Input::get($name);
        if($name){
            $this->user->$name = $v;
        }
    }
    
    public function editPassword(){
        $mobile = Input::get('mobile');
        $code = Input::get('code');
        $user = $this->userDao->findByMobile($mobile);
        if($user){
            if(empty($code) || ($code != Session::get('code'))){
                throw new \Exception('缺少验证码');
            }
            $password = Input::get('password');
            if($password){
                $this->user->password = md5($password);
            }
            $this->user->save();
            $u = $this->user->find($this->user->id);
            return $this->json($this->user);
        }
        return $this->error('没有用户');
    }
    
    public function detail($id){
        $user = $this->user->find($id);
        if($user){
            return $this->json($user);
        }
        return $this->error('没有用户');
    }
    
    
}